//
//  HotCourseCell.m
//  iOS实战
//
//  Created by clz on 2019/10/26.
//  Copyright © 2019 clz. All rights reserved.
//

#import "HotCourseCell.h"
#import <Masonry.h>
#import "Commont.h"
#import <UIImageView+WebCache.h>

@interface HotCourseCell()

@property(nonatomic,strong)UIImageView *itemImageView;
@property(nonatomic,strong)UILabel *itemTitleLabel;
@property(nonatomic,strong)UILabel *itemInfoLabel;

@end

@implementation HotCourseCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.hotCourseCellVM = [[HotCourseCellVM alloc]init];
        [self.hotCourseCellVM addObserver:self forKeyPath:@"picUrl" options:NSKeyValueObservingOptionNew context:nil];
        [self.hotCourseCellVM addObserver:self forKeyPath:@"courseText" options:NSKeyValueObservingOptionNew context:nil];
        [self.hotCourseCellVM addObserver:self forKeyPath:@"courseInfo" options:NSKeyValueObservingOptionNew context:nil];

        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.itemImageView = [[UIImageView alloc]init];
        self.itemImageView.image = [UIImage imageNamed:@"default_banner"];
        self.itemImageView.clipsToBounds = YES;
        self.itemImageView.layer.cornerRadius = 4.0f;
        [self.contentView addSubview:self.itemImageView];
        [self.itemImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(15.5);
            make.bottom.equalTo(self.contentView).offset(-15.5);
            make.left.equalTo(self.contentView).offset(20);
            make.size.mas_equalTo(CGSizeMake(100, 76));
        }];
        
        self.itemTitleLabel = [[UILabel alloc]init];
        self.itemTitleLabel.text = @"测试title";
        self.itemTitleLabel.textColor = RGBHex(0x263238);
        self.itemTitleLabel.font = [UIFont boldSystemFontOfSize:16];
        self.itemTitleLabel.numberOfLines = 2;
        [self.contentView addSubview:self.itemTitleLabel];
        [self.itemTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.itemImageView).offset(7.5 - 3);
            make.left.equalTo(self.itemImageView.mas_right).offset(12);
            make.right.equalTo(self.contentView).offset(-20);
        }];
        
        self.itemInfoLabel = [[UILabel alloc]init];
        self.itemInfoLabel.text = @"测试info";
        self.itemInfoLabel.textColor = RGBHex(0x90A4AE);
        self.itemInfoLabel.font = [UIFont systemFontOfSize:13];
        [self.contentView addSubview:self.itemInfoLabel];
        [self.itemInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.itemImageView.mas_right).offset(12);
            make.right.equalTo(self.contentView).offset(-20);
            make.bottom.equalTo(self.itemImageView).offset(-5);
        }];
        
    }
    return self;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    id value = change[NSKeyValueChangeNewKey];
    
    if ([keyPath isEqualToString:@"picUrl"]) {
        NSString *picUrl = value;
        [self.itemImageView sd_setImageWithURL:[NSURL URLWithString:picUrl] placeholderImage:[UIImage imageNamed:@"default_banner"]];
        
    }else if ([keyPath isEqualToString:@"courseText"]){
        NSString *courseText = value;
        self.itemTitleLabel.text = courseText;
        
    }else if ([keyPath isEqualToString:@"courseInfo"]){
        NSString *courseInfo = value;
        self.itemInfoLabel.text = courseInfo;
    }
}

- (void)dealloc{
    [self.hotCourseCellVM removeObserver:self forKeyPath:@"picUrl"];
    [self.hotCourseCellVM removeObserver:self forKeyPath:@"courseText"];
    [self.hotCourseCellVM removeObserver:self forKeyPath:@"courseInfo"];

}


@end
